package com.zch.house.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zch.house.dao.HouseDao;
import com.zch.house.entity.House;
import com.zch.house.service.HouseService;
import com.zch.house.util.XmlSession;

public class HouseServiceImpl extends XmlSession implements HouseService {
	// 初始化一页的数据条数
	private final int PAGE_SIZE = 20;

	@Override
	public List<House> getAllHouseByPage(String area, String estate, String acreage, double lowPrice,
			double highPrice, int status, int pageIndex) {
		// 使用map集合来存放参数
		Map<String, Object> map = new HashMap<>();
		map.put("area", area);
		map.put("estate", estate);
		map.put("acreage", acreage);
		map.put("lowPrice", lowPrice);
		map.put("highPrice", highPrice);
		map.put("status", status);
		int firstRow = (pageIndex - 1) * PAGE_SIZE + 1;
		int lastRow = pageIndex * PAGE_SIZE;
		map.put("firstRow", firstRow);
		map.put("lastRow", lastRow);
		
		//加载配置文件
		initSession();
		//获得mapper的代理类
		HouseDao dao = session.getMapper(HouseDao.class);
		//调用dao层方法
		List<House> list = dao.getAllHouse(map);
		//提交事务并关闭session
		closeSession();
		return list;
	}

	@Override
	public int getTotalCount(String area, String estate, String acreage, double lowPrice, double highPrice,
			int status) {
		// 使用map集合来存放参数
		Map<String, Object> map = new HashMap<>();
		map.put("area", area);
		map.put("estate", estate);
		map.put("acreage", acreage);
		map.put("lowPrice", lowPrice);
		map.put("highPrice", highPrice);
		map.put("status", status);
		
		//加载配置文件
		initSession();
		//获得mapper的代理类
		HouseDao dao = session.getMapper(HouseDao.class);
		// 调用dao层方法获得查询到的所有数据的个数
		int counts = dao.getTotalCount(map);
		//提交事务并关闭session
				closeSession();
		//计算总页数
		int totalPages = counts%PAGE_SIZE==0?counts/PAGE_SIZE:counts/PAGE_SIZE + 1; 
		return totalPages;
	}

	@Override
	public House getHouseById(int h_id) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		HouseDao dao = session.getMapper(HouseDao.class);
		//调用dao层方法
		House house = dao.getHouseById(h_id);
		//提交事务并关闭session
		closeSession();
		return house;
	}

	@Override
	public boolean addHouse(House house) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		HouseDao dao = session.getMapper(HouseDao.class);
		//调用dao层方法
		boolean flag = dao.addHouse(house);
		//提交事务并关闭session
		closeSession();
		return flag;
	}

	@Override
	public boolean deleteHouse(int h_id) {
		House house = getHouseById(h_id);
		house.setH_status(0);
		boolean flag = updateHouse(house);
		if(flag){
			return true;
		}
		return false;
	}

	@Override
	public boolean updateHouse(House house) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		HouseDao dao = session.getMapper(HouseDao.class);
		//调用dao层方法
		boolean flag = dao.updateHouse(house);
		//提交事务并关闭session
		closeSession();
		return flag;
	}

	@Override
	public int getTotalCount2(String area, String estate, String acreage, double lowPrice, double highPrice,
			int status) {
		// 使用map集合来存放参数
		Map<String, Object> map = new HashMap<>();
		map.put("area", area);
		map.put("estate", estate);
		map.put("acreage", acreage);
		map.put("lowPrice", lowPrice);
		map.put("highPrice", highPrice);
		map.put("status", status);
		
		//加载配置文件
		initSession();
		//获得mapper的代理类
		HouseDao dao = session.getMapper(HouseDao.class);
		// 调用dao层方法获得查询到的所有数据的个数
		int counts = dao.getTotalCount(map);
		//提交事务并关闭session
		closeSession();
		return counts;
	}

	@Override
	public boolean deleteAllhouse(String idStrs,String splitOper) {
		String[] strs = idStrs.split(splitOper);
		System.out.println(Arrays.toString(strs));
		int[] ids = new int[strs.length];
		for(int i = 0; i < ids.length; i++){
			ids[i] = Integer.parseInt(strs[i]);
		}
		System.out.println(Arrays.toString(ids));
		return false;
	}
	public static void main(String[] args) {
		HouseServiceImpl hsi = new HouseServiceImpl();
		hsi.deleteAllhouse("1/45/125/12/358/", "/");
	}

}
